/* 
 * This class creates a file with the name of constructor parameter. 
 * 
 */
package cth.tda367.beavers.glossaryGame.IO;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.util.ArrayList;

import cth.tda367.beavers.glossaryGame.model.Score;
import cth.tda367.beavers.glossaryGame.model.WordList;

public class FileCreator {
	
	private String fileName;
	private WordList wordList;
	private String buffert; 
	ArrayList<Score> points = new ArrayList<Score>();
	
	public FileCreator (String fileName, WordList wordList) {		
		this.fileName = "GlossaryGame/"+fileName;
		this.wordList = wordList;
		create();
	}
	public FileCreator (ArrayList<Score> points, String fileName) {
		this.points = points;
		this.fileName = fileName;
		create();
	}
	
	public String getFileName () {
		return fileName + ".txt";
	}
	
	public void createFromString () {
		try{
		     
		    FileWriter fstream = new FileWriter("c:/"+fileName +".txt");
		    BufferedWriter out = new BufferedWriter(fstream);
		        	        	
		    out.write(buffert);
		    out.close();
		}
	      
	      catch (Exception e){	      
		    	System.err.println("Felmedelande: " + e.getMessage());
		  }
	}
	
	public void create() {
		
		try{
			FileWriter fstream = new FileWriter("c:/"+fileName +".txt", true);
		    BufferedWriter out = new BufferedWriter(fstream);
		    
		    if(fileName.equals("oHighScore") || fileName.equals("hHighScore") || fileName.equals("mHighScore")){
		    	System.out.println("Inside Highscore");
	    	
		    	for(int i = 0; i < points.size(); i++) {
		    		out.write(points.get(i).getName() + " : "+ points.get(i).getPoint());
		    		out.newLine();
		    	}
		    }
		    else {
	    	
		    	for (int i = 0; i < wordList.getSize(); i++) {
	        
		    		if(wordList.getWord(i).getLanguageFirst() != null && wordList.getWord(i).getLanguageSecond() != null) {
		    			out.write(wordList.getWord(i).getLanguageFirst() + " " + wordList.getWord(i).getLanguageSecond() + "\n");
		    			out.newLine();
		    		}
		    	}
	    }
	    
	    
	    out.close();

	}
      
      catch (Exception e){	      
	    	System.err.println("Error: " + e.getMessage());
	    }
	  }
}

